/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/bignum/bignum.h,v 1.3 2003/11/21 12:39:24 itsme Exp $
 *
 * Declaration of the Number class
 *
 *
 */

#ifndef __BIGNUM_H__

#include "gmp/gmp.h"

#include "vectorutils.h"
#include <string>

class Number {
public:
    mpz_t value;

    // only define default constructor and copy constructor
    Number();
    Number(const Number& n);

    // 'explicit' makes sure nu implicit type conversions take place
    explicit Number(int n);
    ~Number();

    // other constructors are defined as class-methods.
    static Number FromInteger(int n);
    int ToInteger() const;


    void FromByteVector(const ByteVector& buf);
    ByteVector ToByteVector() const;

    void setbit(int bitnr);
    bool getbit(int bitnr) const;
    const Number& operator = (const Number& b);
    const Number& operator = (int b);

    Number& operator += (const Number& b);
    Number& operator -= (const Number& b);
    Number& operator *= (const Number& b);
    Number& operator /= (const Number& b);
    Number& operator %= (const Number& b);
    Number& operator ^= (const Number& b);

    Number& operator += (int b);
    Number& operator -= (int b);
    Number& operator *= (int b);
    Number& operator /= (int b);
    Number& operator %= (int b);
    Number& operator ^= (int b);


    int NrOfBytesInNumber() const;
    int NrOfBitsInNumber() const;
    int NrOfDecimalDigitsInNumber() const;

    std::string AsString(int base=10) const;

    int Compare(const Number& b) const;
    int Compare(int intvalue) const;

    Number operator - () const;
    Number operator + (const Number& b) const;
    Number operator - (const Number& b) const;
    Number operator * (const Number& b) const;
    Number operator % (const Number& b) const;
    Number operator / (const Number& b) const;
    Number operator ^ (const Number& b) const;

    Number operator + (int b) const;
    Number operator - (int b) const;
    Number operator * (int b) const;
    Number operator % (int b) const;
    Number operator / (int b) const;
    Number operator ^ (int b) const;

    bool operator == (const Number& b) const;
    bool operator != (const Number& b) const;
    bool operator < (const Number& b) const;
    bool operator <= (const Number& b) const;
    bool operator > (const Number& b) const;
    bool operator >= (const Number& b) const;

    bool operator == (int intvalue) const;
    bool operator != (int intvalue) const;
    bool operator < (int intvalue) const;
    bool operator <= (int intvalue) const;
    bool operator > (int intvalue) const;
    bool operator >= (int intvalue) const;

    Number GCD(const Number& b) const;
    Number ModularPower(const Number& exponent, const Number& modulus) const;
    int ModularCompare(int b, const Number& modulus) const;

    bool IsPerfectPower() const;

    Number SquareRoot() const;
};

bool testNumberClass();

#define __BIGNUM_H__
#endif

